<?php


namespace designpatterns\observer\other;



use designpatterns\observer\Observer;
use designpatterns\observer\Subject;

/**
 * 具体主题
 * Class NewsPublisher
 * @package designpatterns\observer\other
 */
class NewsPublisher implements Subject
{
    private $observers = [];
    private $latestNews;

    public function attach(Observer $observer) {
        $this->observers[] = $observer;
    }
    public function detach(Observer $observer)
    {
        $index = array_search($observer,$this->observers);
        if($index !== false){
            unset($this->observers[$index]);
        }
    }
    public function notify()
    {
        foreach ($this->observers as $observer){
            $observer->update($this->latestNews);
        }
    }
    public function publishNews($new)
    {
        $this->latestNews = $new;
        $this->notify();
    }
}